<template>
	<view class="login-container">
		<view class="login-form">
			<text class="login-title">注册</text>
			<view class="login-input"></view>
			<input class="input" type="text" placeholder="请输入用户名" v-model="username"/>
			<input class="input" type="text" placeholder="请输入密码" v-model="password" password/>
			<input class="input" placeholder="请确认密码" password v-model="confirmPassword" />
			<button class="login-btn" type="primary" @click="handleRegister">注册</button>
			<view class="resgister">
				<navigator url="/pages/login/login">已有账号？去登录</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	import CustomTextView from"../login/login.vue"
	export default {
		data() {
			return {
				username: '',
				password: '',
				confirmPassword: ''
			}
		},
		methods: {
			handleRegister() {
				if (!this.username || !this.password || !this.confirmPassword) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					})
					return
				}
				if (this.password !== this.confirmPassword) {
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none'
					})
					return
				}
				console.log("点击注册，准备调用接口发送数据");
				// 开启注册流程
				uni.showLoading({
					title: '注册中...'
				});
				// 把数据提交到服务端，所有接口的统一调用方式
				uniCloud.callFunction({
					//name 调用的接口名字
					name: 'addUser',
					// 上传的数据
					data: {
						'name': this.username,
						'password': this.password
					},
					success: (msg) => {
						console.log("addUser CallBack " + JSON.stringify(msg));
						//UI更新
						setTimeout(() => {
							uni.hideLoading()
							uni.showToast({
								title: '注册成功'
							})
						}, 1000)
						uni.navigateBack();
					},
					fail: (res) => {
						console.warn("addUser CallBack fail" + res);
					}
				})
			}
		}
	}
</script>


<style>

</style>